import { defineStore } from "pinia";
import type { userInfoType } from "_s/config/user/type";
import { pinia } from "_s/stores/index";
import common from "_s/common/localstore";

export const useUserStore = defineStore("user", () => {
  const userInfo = ref({} as userInfoType);

  const roleMenu = ref<string[]>([]);
  const setRoleMenu = (value: string[]) => (roleMenu.value = value);
  const roleBtns = ref<string[]>([]);
  const setRoleBtns = (value: string[]) => (roleBtns.value = value);

  const isAdmin = ref(false);
  const setUserInfo = (user: userInfoType) => {
    userInfo.value = user;
    isAdmin.value = user.auth == 0;
    // isRouterReady.value = user.auth == 0;
  };

  const logout = () => {
    roleBtns.value = [];
    roleMenu.value = [];
    userInfo.value = {} as userInfoType;
    common.setToken("");
  };

  return { userInfo, /*roles, setRoles ,*/ isAdmin, setUserInfo, roleMenu, setRoleMenu, roleBtns, setRoleBtns, logout };
});

export function useUserHook() {
  return useUserStore(pinia);
}
